﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace WfTimedExecService
{
    public class Url_Request
    {
        public string UrlRequest(string Url)
        {
            //WebClient client = new WebClient();
            try
            {
                Stream outstream = null;
                Stream instream = null;
                StreamReader sr = null;
                HttpWebResponse response = null;
                HttpWebRequest request = null;
                Encoding encoding = Encoding.UTF8;
                //byte[] data = encoding.GetBytes(SendRedEnvelopesData);

                // 设置参数
                request = WebRequest.Create(Url) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                //request.ContentLength = data.Length;
                outstream = request.GetRequestStream();
                //outstream.Write(data, 0, data.Length);
                outstream.Close();
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                string ResponseStream = sr.ReadToEnd();

                return ResponseStream;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
